home *** CD-ROM | disk | FTP | other *** search
/ Amiga Mag HDD Backup / Amiga Mag HDD Backup.zip / Amiga Mag HDD Backup / Alexander.img.bin / Alexander.img / ***9.11 All NEWer important / texts / Nee⁄3 Assembly / Article1.txt next >
Text File  |  1983-05-04  |  8KB  |  41 lines

  1.  
  2. Part 1 - Getting Started
  3.  
  4.      Welcome to assembly language programming. In the next several articles I'll cover the major machine language programming techniques for the Amiga - libraries, screens, windows, graphics, sound, menus, gadgets, math co-processor and other topics. In addition each article will have a working program for you to assemble that demonstrates that particular topic. I'll be using the PHXASS assembler, a Public Domain program available from many sources (Fish Disk #749); I like this assembler for some reasons I'll discuss in a future article and I suggest you start now to get a copy so you can follow along. Other assemblers will probably work with minor adjustments.
  5. WHY ASSEMBLY LANGUAGE?
  6.      Assembly language code communicates directly with the computer. You have to write your program in great detail, but the speed at which it works will astound you. A well-written machine language program will run 10 times faster that a Basic one and two to five times faster than most compiled ones. It does take time to learn assembly procedure and techniques but once you have a good routine it can be saved and used later in other programs. And there are even some things you can do in assembly language that are almost impossible to do in Basic. Take the time to follow these articles and listings and I think you'll be quite impressed with what you can do using assembly language. Now let's get down to some basics.
  7. GOOD NEWS AND BAD NEWS
  8.      The bad news is that assembly language uses math; the good news is that there are only two digits - 0 and 1. These two digits are the foundation for the Base 2 (binary) system. We humans normally use Base 10 (decimal) numbers, but programmers thrive on Base 2 and it's cousin Base 16 (hexadecimal). The rules for binary math are very simple -
  9.  ADDITION        SUBTRACTION       MULTIPLICATION     DIVISION
  10.  0   0   1      0   1   1   10       0   1   1        0   1   1
  11. +0  +1  +1     -0  -1  -0   -1      x0  x0  x1       /1  /1  /0
  12.  0   1  10      0   0   1    1       0   0   1        0   1   OOPS!!!
  13.      Since each number in the binary system is a power of 2 it's very simple to convert numbers from our decimal system to binary and vice versa. For example, the binary number 1011 represents 1*23+0*22+1*21+1*20 or 11 decimal. The decimal number 30 can be represented in powers of two as 16+8+4+2, so in binary it's 11110.
  14.      Each 0 or 1 is called a bit and eights bits in a row is a byte;
  15. the bits, however, are numbered from right to left as bit0 to bit7. With eight available bits, one byte could contain a maximum value of 11111111 or 255 decimal. Two bytes in a row (bit15 to bit0) make up a word and two words in a row are a double or long word. Quick, what's the maximum decimal value of a word?
  16.      Because binary numbers can get rather large, programmers found it easier to use the Base 16 or Hex to represent values. Since there must be sixteen numbers in this base, the letters A through F are used to represent decimal 10 through 15. To rapidly convert from binary to Hex, arrange the binary number in groups of four adding zeroes to the left group if necessary; then write the Hex equivalent of each group. For example, convert 111101111011110 to a Hex number - 
  17.   binary   (0111)  (1011)  (1101)  (1110)
  18.   decimal    (7)    (11)    (13)    (14)
  19.   Hex         7       B       D       E    = 7BDE
  20. What three values could "11" represent?
  21. WHAT ABOUT NEGATIVE NUMBERS?
  22.      This works well with regular positive numbers, but how is a value such as -10 represented? The computer thinks of all numbers as being on a wheel; reading the numbers to the right reads them in a positive direction but reading to the left indicates a negative value. Let's use a range of 0 to 255 or 0 to FF. Starting at 0 and reading to the right indicates 1, 2, 3..., but going to the left reads FF, FE, FD, etc. Since the first value to the left is FF, it must represent -1; the next value FE represents -2, and so forth. The middle of the wheel is the boundary between positive and negative values. The positive values range from 0 to 7F and the negative from FF to 80. Using negative numbers cuts the effective range of values in half; instead of 0 to 255 signed numbers have a range of -128 to +127.
  23.      Time out! Isn't FF also 255 in decimal? Yes, it is. How does the computer know if we want FF to represent -1 or 255? The answer is that it doesn't care! The only difference to the computer between positive and negative numbers is that in a negative number the left-most bit (called the most significant bit - MSB) must be set; that is, it must be a 1. The MSB of any positive number is always 0. If the MSB is set, a flag within the computer is also set. When we discuss assembly commands later on you'll see that there are different commands for signed (positive and negative values) or unsigned values (only positive). The computer will let you know what you have, but it's up to the programmer to interpret the result.
  24.      For now, remember that if you need to use signed values, your range of numbers is cut in half. If you want a positive value greater than 127 you would probably use a word to hold the value. Now the numbers from 0 to 7FFF are positive and those from FFFF to 8000 are negative. What are the largest positive and smallest negative word values?
  25. AND, OR, ETC.
  26.      In addition to the arithmetic operations in the binary system there is also a set of Boolean operations for you to use. Named for mathematician George Boole these operations include AND, OR, Exclusive OR, NOT, etc. You probably won't use any more that the first three of these, but here are the rules for several functions -
  27.     AND           OR          EOR        NOT        NEG
  28.   0  0  1      0  0  1      0  0  1      0  1      1  -1
  29.   0  1  1      0  1  1      0  1  1                      
  30.   0  0  1      0  1  1      0  1  0      1  0     -1   1
  31. At first, these rules don't look that informative, but think of them this way -
  32.   X AND 0 = 0    X AND 1 = X  
  33.   X OR 0 = X     X OR 1 = 1     
  34.   X EOR X = 0    X EOR Y = 1
  35.      The AND rule, for example, could be used to keep a number between 0 and 7; any number AND 7 will always be within this range. You can also force a specific bit to 0. If we want bit1 in a word to be 0, AND the word with 11111101 or FD. All the values remain the same except for bit1 which will always go to 0. In the same manner you can set any bit using OR; if the MSB in a byte must always be 1 OR the word with 10000000 or 80. You can even combine AND with OR to produce specific required results.
  36.      The EOR function is very interesting and is also the basis for simple codes. Since same values EOR'd result in 0 and different values EOR'd produce 1, it can be used to toggle values. Keep EOR'ing a counter with 1 and react differently if the result is 0 or 1. This function is also helpful in multiplication or division where a positive times positive, or negative times negative result must be positive while a positive times negative result must be negative. Since opposites result in a 1, repeating the EOR on one of the original numbers will produce the other number. In other words; X EOR Y = Z, Y EOR Z = X and X EOR Z = Y. Try 11 EOR 9 and then EOR that result with 11 or 9.
  37. CODING A MESSAGE
  38.      You can make a simple code using the EOR principle. First, pick a value known only to you and the recipient of your code. Then EOR the ASCII value of each letter in your code with the secret value and send the result to the other person. The recipient now only has to EOR the values in the code with the secret value and then print the ASCII values or letters. If the code value is 15, what would the message "]JNK/NBNUFAH/BNHNUFAJ" decode to?
  39.      One final operation that is used in many programs is MOD. This produces the result or remainder after dividing by a specific number. The equation 11 MOD 7 would equal 4 since division (11/7) gives a result of 1 with a remainder of 4. How do 17 AND 15 and 17 MOD 15 compare? What about 17 AND 15 and 17 MOD 16?
  40.      There's no assembly language program this month. But use the time until the next article to get an assembler. I guarantee you'll be writing, testing, and using an assembly language program within ten minutes of reading the next article. Well, maybe fifteen.  
  41.